/*
 * Copyright (c) 2015 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShPropertyFeeDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShPropertyFeeService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShPropertyFee;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * ShPropertyFeeService 实现类
 * 
 * @author KongXiangyu
 * @since 2015-09-07
 */
@Service("shPropertyFeeService")
public class ShPropertyFeeServiceImpl extends
		BaseServiceImpl<ShPropertyFee, Long> implements ShPropertyFeeService {

	@Resource
	private ShPropertyFeeDao shPropertyFeeDao;

	public BaseDao<ShPropertyFee, Long> getDao() {
		return shPropertyFeeDao;
	}

	@Override
	public DataGrid queryShPropertyFees(PageHelper page, Long numId) {
		DataGrid dataGrid = new DataGrid();
		ShPropertyFee query = new ShPropertyFee();
		query.setDoorId(numId);
		long totalCount = shPropertyFeeDao.selectEntryListCount(query);
		dataGrid.setTotal(totalCount);
		int startIndex = (page.getPage() - 1) * page.getRows();
		int endIndex = page.getPage() * page.getRows();
		if (totalCount > 0 && totalCount > startIndex) {
			query.setStartIndex(startIndex);
			query.setEndIndex(endIndex);
			query.setOrderField("f.id");
			query.setOrderFieldType("desc");
			List<ShPropertyFee> shPropertyFee = shPropertyFeeDao
					.selectEntryListByDoorId(query);
			dataGrid.setRows(shPropertyFee);
		}
		return dataGrid;
	}

	@Override
	public List<ShPropertyFee> getByDoorIdAndYear(ShPropertyFee fee) {
		return shPropertyFeeDao.getByDoorIdAndYear(fee);
	}

	@Override
	public List<ShPropertyFee> getByDoorIdAndMonth(ShPropertyFee fee) {
		return shPropertyFeeDao.getByDoorIdAndMonth(fee);
	}

	@Override
	public DataGrid queryFeesGroupByDoor(ShPropertyFee queryPropertyFee, PageHelper page) {
		DataGrid grid = new DataGrid();

		if (!StringUtils.isBlank(page.getOrder())) {
			queryPropertyFee.setOrderFieldType(page.getOrder());
		}
		if (!StringUtils.isBlank(page.getSort())) {
			queryPropertyFee.setOrderField(page.getSort());
		}

		int startIndex = (page.getPage() - 1) * page.getRows();
		int endIndex = page.getPage() * page.getRows();
		Long count = shPropertyFeeDao.queryFeesGroupByDoorCount(queryPropertyFee);
		grid.setTotal(count);

		if (grid.getTotal() > 0 && grid.getTotal() > startIndex) {
			queryPropertyFee.setStartIndex(startIndex);
			queryPropertyFee.setEndIndex(endIndex);
			List<ShPropertyFee> rows = shPropertyFeeDao.queryFeesGroupByDoor(queryPropertyFee);
			grid.setRows(rows);
		}

		return grid;
	}

	@Override
	public Long getAmountSumByResidence(ShPropertyFee propertyFee) {
		return shPropertyFeeDao.queryAmountSumByResidence(propertyFee);
	}

}